﻿using UnityEngine;
using System.Collections;
using NetWork;
using AssetLoad;
using UnityEngine.SceneManagement;

namespace Room
{
    /// <summary>
    /// 公会
    /// </summary>
    public class RoomUnion : RoomBase
    {
        public string sceneMap;
        public override SceneType GetSceneType()
        {
            return SceneType.UNION;
        }

        public override ServerType GetServerType()
        {
            return ServerType.union;
        }

        public override void Init()
        {
            base.Init();
        }

        public override void InitRoomNet()
        {
            netManager = new RoomNetUnion();
            netManager.Init(this);
        }

        protected override void OnEnter(params object[] param)
        {
            base.OnEnter(param);


            StartCoroutine(_WaitForLoadComplete());
            ManagerController.Instance.cameraTool.SwitchSceneCamera();

            Foreground();
        }

        private IEnumerator _WaitForLoadComplete()
        {
            sceneMap = SceneName.union;
            if (SceneManager.GetActiveScene().name != sceneMap)
            {
                var scenePath = SceneName.sceneFolder + sceneMap;
                yield return StartCoroutine(AssetBundleManager.Instance.LoadSceneAsync(scenePath, sceneMap));
            }
            OnLoadComplete();
        }


        protected void OnLoadComplete()
        {
            netManager.Enter();
            smallMap.ShowMiniMap();
            smallMap.SetData(Dict.Blo.TableSceneBlo.GetSceneById(sceneMap));

            LoadNPCDataFromTable("union");
            LoadAreaDataFromTable("union");

            sceneCamera.Set(cameraDelta, new Vector3(cameraAngle, 0, 0), cameraFOV);
            StaticData.cameraRotate = Vector3.zero;
            CameraTool.Instance.ResetSceneBloomEffect();

            ManagerController.Instance._CompleteTownToUnion();
        }

        protected override void OnLeave()
        {
            base.OnLeave();
            charactorManager.ClearAll();
            netManager.Leave();
            smallMap.HideMiniMap();
        }

        protected override void Tick()
        {
            base.Tick();

            netManager.Tick();
            smallMap.Tick();
            viewController.Tick();
            charactorManager.Tick();
            sceneCamera.Set(cameraDelta, new Vector3(cameraAngle, 0, 0), cameraFOV);
        }

        protected override void OnForeground()
        {
            base.OnForeground();
            netManager.Resume();
            viewController.Resume();
            uiManager.gameObject.SetActive(true);
            EasyTouchTool.Instance.ShowJoystick();
        }

        protected override void OnBackground()
        {
            base.OnBackground();
            netManager.Pause();
            viewController.Pause();
            uiManager.gameObject.SetActive(false);
            EasyTouchTool.Instance.Hide();
        }

        public override void OnMainCharactorLoaded(Transform mc)
        {
            base.OnMainCharactorLoaded(mc);
            viewController.SetPlayer(mc);
            smallMap.miniMapComponent.SetMajorCharactor(mc);
        }
        public override void PlayerSay(string playerId, string word)
        {
            var charactor = charactorManager.GetCharactor(playerId);
            if (charactor != null)
            {
                charactor.ShowBubble(word);
            }
        }

        public override void LoadNPCDataFromTable(string name)
        {
            var listNpcs = Dict.DictDataManager.Instance.tableNPCDao.GetByOneIndex(name);
            foreach (var npc in listNpcs)
            {
                switch (npc.type)
                {
                    case (int)NPCType.Union:
                        charactorManager.CreateNPC(npc.npcId);
                        break;
                }
            }
        }

        /// <summary>
        /// 英雄雕像
        /// </summary>
        /// <param name="opend"></param>
        public void SetHeroStatue(bool opend)
        {
            //
            var str0 = @"/particle/guild_obj_401a_TX";
            var obj = GameObject.Find(SceneManager.GetActiveScene().name + str0);
            if (obj != null)
            {
                obj.SetActive(opend);
            }


            var str1 = @"/models/guild_models_005/guild_obj_401/guild_obj_401a";
            var md = GameObject.Find(SceneManager.GetActiveScene().name + str1);
            if (md)
            {
                var mat = Resources.Load<Material>(opend ? "shaders/guild_obj_401a" : "shaders/guild_obj_401");
                if (mat != null)
                {
                    md.GetComponent<MeshRenderer>().materials[0] = mat;
                }
                var tex = Resources.Load<Texture>( opend? "shaders/guild_obj_401a":"shaders/guild_obj_401");
                if (tex != null && mat != null)
                {
                    md.GetComponent<MeshRenderer>().materials[0].mainTexture = tex;
                }

                Resources.UnloadUnusedAssets();
            }
        }

        /// <summary>
        /// 守护之树
        /// </summary>
        /// <param name="opend"></param>
        public void SetGuardTree(bool opend)
        {
            var obj = GameObject.Find(SceneManager.GetActiveScene().name+@"/particle/shuguang");
            if(obj!=null)
            {
                obj.SetActive(opend);
            }
        }
    }
}